
function [ frame, new_flag, total_frame, num_in_stream, num_out_stream, eof_flag, cur_file_name, cur_pattern ] = get_frame( file_name, pattern, start_num)
%   DWang
%   create @ 06/07/2010
%   get_frame using mmread to generate pull down stream for MEMC algorithm
%   stream_file_name : file name for the stream file
%   repeat_pattern: output stream pattern, used for FRC. 
%   0: means need notread new frame; 
%   1:means need read new frame, first element must be 1
%   start_num: start frame in stream, it should > 0
%   usage: 
%   step 1, [ frame, num_in_stream, num_out_stream, eof_flag, ...
%   cur_file_name, cur_pattern ] = getFrame( file_name, pattern )
%   set stream file name and pull down pattern
%   step 2, call getFrame directly to get picture one by one
%   example: 
%   frame = get_frame('test.mpg', [10010], 200);
%   frame = get_frame();
mlock;
persistent repeat_pattern;
persistent stream_file_name;
persistent idx_in_ptn;
persistent idx_in_stream;
persistent idx_in_clip;
persistent idx_out_stream;
persistent video_clip;
persistent total_frm_num;
persistent new_mode;

new_frame = 0;

switch (nargin)
    case 0, 
        if isempty(repeat_pattern) || isempty(stream_file_name)
            uiwait(msgbox('need initial file_name and pattern','UnInitial Parameters','modal'));
            frame=[]; num_in_stream=[]; num_out_stream=[]; eof_flag=[]; cur_file_name=[]; cur_pattern=[];
            return;
        end
    case 1,
        stream_file_name = file_name;
        if isempty(repeat_pattern)
            uiwait(msgbox('need set repeat pattern','Lost Second Parameter','modal'));
            frame=[]; num_in_stream=[]; num_out_stream=[]; eof_flag=[]; cur_file_name=[]; cur_pattern=[];
            return;
        else
            idx_in_ptn = 0;
            idx_in_stream = 0;
            idx_in_clip = 0;
            idx_out_stream = 0;
%             vid = mmreader(stream_file_name);
%             if isempty(vid.NumberOfFrames) %variable frame rate stream
%                 read(vid, inf);
%             end
            total_frm_num = get_frame_number(file_name);%vid.NumberOfFrames;
            if(total_frm_num == 0)
                display('can not determin the frame number');
            end
        end
        frame = [];
        num_in_stream = idx_in_stream;
        eof_flag = (num_in_stream == total_frm_num);
        num_out_stream = idx_out_stream;
        cur_file_name = stream_file_name;
        cur_pattern   = repeat_pattern;
        return;
    case {2, 3}
        stream_file_name = file_name; repeat_pattern = pattern;
        idx_in_ptn = 0;
        if(nargin==3)
            idx_in_stream = start_num-1;
        else
            idx_in_stream = 0;
        end
        idx_in_clip = 0;
        idx_out_stream = 0;
%         vid = mmreader(stream_file_name);
%         if isempty(vid.NumberOfFrames) %variable frame rate stream
%             read(vid, inf);
%         end
%         total_frm_num = vid.NumberOfFrames; 
        mminfo = mmfileinfo(file_name);
        if(isequal(mminfo.Video.Format, 'IV50'))
            new_mode = false;
        else
            new_mode = true;
        end
        total_frm_num = get_frame_number(file_name, new_mode);%vid.NumberOfFrames;
        if(total_frm_num == 0)
            display('can not determin the frame number');
        end
        frame = [];
        num_in_stream = idx_in_stream;
        eof_flag = (num_in_stream == total_frm_num);
        num_out_stream = idx_out_stream;
        cur_file_name = stream_file_name;
        cur_pattern   = repeat_pattern;
        return;
    otherwise,
        uiwait(msgbox('no more than 2 parameters','Parameter Number Error','modal'));
        return;
        
end

clip_length = 20;

idx_in_ptn = idx_in_ptn + 1;
if(idx_in_ptn > length(repeat_pattern))
    idx_in_ptn = 1;
end
new_flag = repeat_pattern(idx_in_ptn);

if new_flag
    idx_in_stream = idx_in_stream + 1;
    idx_in_clip   = idx_in_clip + 1;
    if(idx_in_clip > clip_length)
        idx_in_clip = 1;
    end
    if idx_in_clip == 1
        video_clip = mmread(stream_file_name, idx_in_stream:(idx_in_stream+clip_length-1),[],false, true, '', false, new_mode);
    end
end
frame = video_clip.frames(idx_in_clip);
idx_out_stream = idx_out_stream + 1;

num_in_stream = idx_in_stream;
eof_flag = (num_in_stream == total_frm_num);
num_out_stream = idx_out_stream;
cur_file_name = stream_file_name;
cur_pattern   = repeat_pattern;
total_frame   = total_frm_num;


function frame_num = get_frame_number(file_name, new_mode)
vid = mmread(file_name, 1,[],false, true, '', false, new_mode);
if(vid.nrFramesTotal < 0 )
    frame_num = floor(0.9*(-vid.nrFramesTotal));
else
    frame_num = vid.nrFramesTotal;
end


